package com.das.mechanic_base.adapter.processnew;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.alone.X3CameraNewAdapter;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.bean.processsive.ProcessNewBean;
import com.das.mechanic_base.utils.AverageGapItemDecoration;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 施工过程
 */
public class ConstructionProcessAdapter extends RecyclerView.Adapter<ConstructionProcessAdapter.ShowImageHolder>  {

    //    private List<AloneImageAndRecordBean> imageList;
    List<ProcessNewBean.ProcedureTempletEntityListDTO> imageList;

    private List<List<AloneImageAndRecordBean>> pImageList;

    private Context mContext;

    private boolean isShowRequired;

    private int deleteIndex;

    private boolean isShowNotNeedPhoto = false;

    public ConstructionProcessAdapter(List<ProcessNewBean.ProcedureTempletEntityListDTO> imageList,List<List<AloneImageAndRecordBean>> pImageList, Context mContext) {
        this.imageList = imageList;
        this.mContext = mContext;
        this.pImageList = pImageList;
    }

    public void changeRequiredShow(boolean isShowRequired){
        this.isShowRequired = isShowRequired;
        notifyDataSetChanged();
    }

    public boolean isShowNotNeedPhoto() {
        return isShowNotNeedPhoto;
    }

    public void setShowNotNeedPhoto(boolean showNotNeedPhoto) {
        isShowNotNeedPhoto = showNotNeedPhoto;
    }

    @NonNull
    @Override
    public ConstructionProcessAdapter.ShowImageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ConstructionProcessAdapter.ShowImageHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_item_constractionprocess, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ConstructionProcessAdapter.ShowImageHolder holder, final int position) {
        holder.tv_name.setVisibility(X3StringUtils.isEmpty(imageList.get(position).name) ? View.GONE : View.VISIBLE);

        if (imageList.get(position).isCustomeProcess){
            holder.tv_name.setText(X3StringUtils.isEmpty(imageList.get(position).name) ? "" :  imageList.get(position).name +  mContext.getString(R.string.x3_construction_process_suffix));
        } else {
            holder.tv_name.setText(X3StringUtils.isEmpty(imageList.get(position).name) ? "" :  imageList.get(position).name);
        }
        holder.rlv_photo.setLayoutManager(new GridLayoutManager(mContext, 3));

//        if (!imageList.get(position).needPhoto && !isShowNotNeedPhoto) {
        if (!imageList.get(position).needPhoto ) {
            changeShowRequired(holder);
        }else {
            holder.itemView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }

        if (X3StringUtils.isEmpty(imageList.get(position).name)){
            holder.tv_star.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_star.setVisibility(View.VISIBLE);
        }
        holder.tv_star.setText(imageList.get(position).needPhoto ? mContext.getString(R.string.x3_must_input) : mContext.getString(R.string.x3_no_must_input));

        holder.tv_star.setBackgroundColor(imageList.get(position).needPhoto ? Color.parseColor("#100077ff") : ContextCompat.getColor(mContext,R.color.bg_F0F0F5));

        holder.tv_star.setTextColor(imageList.get(position).needPhoto ? Color.parseColor("#0077ff") : Color.parseColor("#b1b3bd"));

        holder.iv_check.setVisibility(X3StringUtils.isListEmpty(imageList.get(position).exampleImgUrlList) ? View.GONE : View.GONE);

        List<AloneImageAndRecordBean> iList = pImageList.get(position);
        if (X3StringUtils.isListEmpty(iList)) {
            iList = new ArrayList<>();
        }
        X3CameraNewAdapter newAdapter = new X3CameraNewAdapter(mContext,true,position,3);
        holder.rlv_photo.addItemDecoration(new AverageGapItemDecoration(10,0,0));

        holder.rlv_photo.setAdapter(newAdapter);
        newAdapter.changeData(iList);
        newAdapter.setiOnItemCameraClick(new X3CameraNewAdapter.IOnItemCameraClick() {
            @Override
            public void iOnItemCameraClick(int titleIndex) {
                if (iOnClickPhoto != null) {
                    iOnClickPhoto.iOnClickTakePicture(position,imageList.get(position).name + position,newAdapter.getItemCount() - 1,newAdapter.getImageList());
                }
            }

            @Override
            public void iOnDeleteClick(long resId, int mPosition, int titleIndex, String path) {
                if(iOnClickPhoto != null){
                    iOnClickPhoto.iOnClickRemovePicture(position,imageList.get(position).name + position,newAdapter.getItemCount() - 1,mPosition);
                }
            }

            @Override
            public void iOnAgainUploadImage(String path, int titleIndex, int imageIndex) {
                if (iOnClickPhoto != null) {
                    iOnClickPhoto.iOnAgainUploadImage(path,titleIndex,imageList.get(position).name + position);
                }
            }

            @Override
            public void iOnDeleteImage(int titleIndex, List<AloneImageAndRecordBean> mList) {
                if (iOnClickPhoto != null) {
                    iOnClickPhoto.iOnDeleteImage(titleIndex,mList,imageList.get(position).name + position);
                }
            }

        });

        // 如何操作
        holder.iv_check.setOnClickListener(v -> {

        });

        if (imageList.get(position).isCustomeProcess || !imageList.get(position).standardOrNot){
            holder.iv_delete.setVisibility(View.VISIBLE);
        } else {
            holder.iv_delete.setVisibility(View.GONE);
        }
        holder.iv_delete.setOnClickListener(v -> {
            if (iOnClickTitle!=null){
                iOnClickTitle.inOnClickDelete(imageList.get(position).name + position,!X3StringUtils.isListEmpty(pImageList.get(position)));
            }
        });

        holder.tv_name.setOnClickListener(v -> {
            if (iOnClickTitle!=null){
                if (imageList.get(position).isCustomeProcess){
                    iOnClickTitle.iOnClickTitle(position,imageList.get(position).name);
                }
            }
        });

    }

    /**
     * 控制是否显示必填项
     * @param newholder
     */
    private void changeShowRequired(RecyclerView.ViewHolder newholder) {
        ViewGroup.LayoutParams itemLayout = newholder.itemView.getLayoutParams();
        if (isShowRequired) {
            itemLayout.height = 0;
        }else {
            itemLayout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        newholder.itemView.setLayoutParams(itemLayout);
    }

    IOnClickPhoto iOnClickPhoto;

    public void setiOnClickPhoto(IOnClickPhoto iOnClickPhoto) {
        this.iOnClickPhoto = iOnClickPhoto;
    }

    public interface IOnClickPhoto {
        void iOnClickTakePicture(int imageIndex,String imageTitle,int imageNum,List<AloneImageAndRecordBean> imgList);
        void iOnClickRemovePicture(int imageIndex, String imageTitle, int num, int mPosition );
        void iOnAgainUploadImage(String path,int titleIndex,String imageName);
        void iOnDeleteImage(int titleIndex, List<AloneImageAndRecordBean> mList,String imageName);
    }

    IOnClickTitle iOnClickTitle;
    public void setIOnClickTitle(IOnClickTitle iOnClickTitle) {
        this.iOnClickTitle = iOnClickTitle;
    }
    public interface IOnClickTitle {
        void iOnClickTitle(int index,String name);
        void inOnClickDelete(String imageName,boolean isHavePicture);
    }

    @Override
    public int getItemCount() {
        return imageList.size();
    }

    class ShowImageHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        private  RecyclerView rlv_photo;
        ImageView iv_check;
        ImageView iv_delete;
        private TextView tv_star;

        public ShowImageHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            rlv_photo = itemView.findViewById(R.id.rlv_photo);
            iv_check = itemView.findViewById(R.id.iv_check);
            tv_star = itemView.findViewById(R.id.tv_star);
            iv_delete = itemView.findViewById(R.id.iv_delete);

        }
    }
}
